package DB;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;


public class ConnectionDB {

    //Use Balder
    private boolean useBalder = true;

    //Driver and database name
    private static String driver;
    private static String databaseName;

    //Credentials
    private static String userName;
    private static String password;

    private DatabaseMetaData dma;
    private static Connection conn;
    private static ConnectionDB instance = null;


    //Private constructor
    private ConnectionDB() {

        if(useBalder)
        {
            driver = "jdbc:sqlserver://balder.ucn.dk";
            databaseName = ";databaseName=dmaa0213_1";
            userName = ";user=dmaa0213_1";
            password = ";password=MaaGodt";

        }
        else
        {
            driver = "jdbc:sqlserver://localhost:1433";
            databaseName = ";databaseName=UCNAttendance";
            userName = ";user=sa";
            password = ";password=masterkey";
        }

        String url = driver + databaseName + userName + password;

        //Trying to load the driver
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Loading of driver - OK");
        }
        catch (Exception e) {
            System.out.println("Loading of driver - FAILED");
            System.out.println(e.getMessage());
        }

        try {
            conn = DriverManager.getConnection(url);
            conn.setAutoCommit(true);

            dma = conn.getMetaData();

            System.out.println("Connection to: " + dma.getURL());
            System.out.println("Driver: " + dma.getDriverName());
            System.out.println("Database product name: " + dma.getDatabaseProductName());

        } catch (Exception e) {
            System.out.println("Problem connecting to the database");
            System.out.println(e.getMessage());
            System.out.println(url);
        }
    }

    //Singleton pattern
    public static ConnectionDB getInstance() {
        if (instance == null) {
            instance = new ConnectionDB();
        }
        return instance;
    }

    //CloseDB
    public static void closeConnection() {
        try {
            conn.close();
            System.out.println("Connection is closed");
        }
        catch (Exception e) {
            System.out.println("Error trying to close the connection" + e.getMessage());
        }
    }

    //Get conn - returns the connection to the database
    public static Connection getConn() {
        return conn;
    }

    //Start transaction
    public static void startTransaction() {
        try {
            conn.setAutoCommit(false);
        }
        catch (Exception e) {
            System.out.println("Error starting transaction");
            System.out.println(e.getMessage());
        }
    }

    //Commit transaction
    public static void commitTransaction() {
        try{
            conn.setAutoCommit(true);
        }
        catch(Exception e){
            System.out.println("Error committing transaction");
            System.out.println(e.getMessage());
        }
    }

    //Rollback transaction
    public static void rollbackTransaction() {
        try{
            conn.rollback();
            conn.setAutoCommit(true);
        }
        catch(Exception e){
            System.out.println("fejl rollback transaction");
            System.out.println(e.getMessage());
        }
    }
}
